package com.ymatou.iapi.trading.sellerquery.parameter;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import com.ymatou.query.seller.trading.api.parameter.Common;
import com.ymatou.query.seller.trading.api.parameter.GetExportProductInfoResult;
import com.ymatou.query.seller.trading.api.parameter.SellerExportedProductInfo;
import com.ymatou.query.seller.trading.api.parameter.Ymt_O_OrderNote;
import com.ymatou.query.seller.trading.api.parameter.Ymt_Order;
import com.ymatou.query.seller.trading.api.parameter.Ymt_OrderExtension;
import com.ymatou.query.seller.trading.api.parameter.Ymt_OrderInfo;
import com.ymatou.query.seller.trading.api.parameter.Ymt_OrderState;
import com.ymttest.utils.DataUtil;

public class GetExportProductInfo {
	public static List<SellerExportedProductInfo> GetExportProductInfos(
			GetExportProductInfoResult result) {
		List<SellerExportedProductInfo> exportedProductInfoList = new ArrayList<SellerExportedProductInfo>();
		if (DataUtil.isNullOrEmpty(result.getOrders())) {
			return exportedProductInfoList;
		}
		for (Ymt_Order order : result.getOrders()) {
			SellerExportedProductInfo export = new SellerExportedProductInfo();
			int iOrderId = order.getiOrderId();
			List<Ymt_OrderInfo> orderInfoList = result.getOrderInfos().stream()
					.filter(x -> x.getiOrderId() == iOrderId)
					.collect(Collectors.toList());
			List<Ymt_OrderState> orderState = result.getOrderStates().stream()
					.filter(x -> x.getiOrderId() == iOrderId)
					.collect(Collectors.toList());
			List<Ymt_O_OrderNote> orderNote = result.getOrderNotes().stream()
					.filter(x -> x.getiOrderId() == iOrderId)
					.collect(Collectors.toList());
			export.setOrderNo(iOrderId);
			export.setFreight(order.getfFreight());
			export.setDepositAmount(order.getfOrderPrice());
			export.setOrderType(DataUtil.Integerr
					.GetValueOrDefault(Ymt_OrderExtension.GetOrderType(order,
							orderInfoList.get(0))));
			export.setOrderState(order.getiTradingStatus());
			export.setRiskVerifiedStatus(order.getiRiskVerifiedStatus());
			export.setIsPaidInFull(order.isbPaidInFull());
			export.setAddTime(order.getdAddTime());
			export.setPayTime(order.getdPaidTime());
			export.setApplyPostPayTime(order.getdApplyPostPayTime());
			export.setLeaveWord(order.getsLeaveWord());
			export.setBuyerLoginId(order.getsBuyerLoginId());
			export.setReceivePerson(order.getsReceivePerson());
			export.setPhone(order.getsPhone());
			export.setTelephone(order.getsTelephone());
			export.setAddress(order.getsAddress());
			export.setPostCode(order.getsPostCode());
			export.setPackageDelivery_DomesticDelivered(Common
					.GetPackageDeliveryDomesticDeliveredStatus(
							DataUtil.Integerr.GetValueOrDefault(order
									.getiTradingStatus()), orderInfoList.get(0)
									.getiCatalogStatus(), order
									.getbDomesticDelivered()));
			export.setOrderNote(DataUtil.isNullOrEmpty(orderNote) == false ? orderNote
					.get(0).getsContent() : DataUtil.Stringg.Empty);
			export.setRemarkLevel(DataUtil.isNullOrEmpty(orderNote) == false ? orderNote
					.get(0).getiRemarkLevel() : null);

			export.setProductNames(DataUtil.Stringg
					.isNullOrEmpty(orderInfoList == null ? null : orderInfoList
							.get(0).getsTitle()) ? DataUtil.Stringg.Empty
					: (orderInfoList == null ? null : orderInfoList.get(0)
							.getsTitle().replace(" ? ", "")));
			export.setProductNum(DataUtil.Integerr
					.GetValueOrDefault(orderInfoList.get(0).getiAmount()));
			export.setProductPrice(orderInfoList.get(0).getfOriginalPrice());
			export.setProductNos(GetProductNos(orderInfoList.get(0).getsSKU()));
			export.setCatalogInfos(GetCatalogInfos(orderInfoList.get(0)
					.getsPropertyInfo()));
			if (DataUtil.isNullOrEmpty(orderState) == false) {
				export.setPostPayAmount(Ymt_OrderExtension
						.GetPostPaidAmount(orderState.get(0)));
				export.setCashAmount(Ymt_OrderExtension
						.GetCurrentAmountOfCash(orderState.get(0)));
				export.setTotalAmount(Ymt_OrderExtension
						.GetOrderCurrentPrice(orderState.get(0)));
				export.setRefundAmount(Ymt_OrderExtension
						.GetRefundedAmount(order,orderState.get(0)));
			}
			exportedProductInfoList.add(export);
		}
		return exportedProductInfoList.stream()
				.sorted((a, b) -> b.getOrderNo() - a.getOrderNo())
				.collect(Collectors.toList());
	}

	// private static void SetOrderInfos(ExportOrderInfo info, Ymt_Order order,
	// List<Ymt_OrderInfo> orderInfos) {
	// info.setOrderType(Ymt_OrderExtension.GetOrderType(order, orderInfos));
	// info.setDeliveryType(Ymt_OrderExtension.GetDeliveryMethod(orderInfos));
	//
	// if (false == DataUtil.isNullOrEmpty(orderInfos)) {
	// Ymt_OrderInfo t = orderInfos.get(0);
	// info.setCatalogStatus(t.getiCatalogStatus());
	// info.setCatalogType(t.getiCatalogType());
	// info.setBondedArea(DataUtil.Integerr.GetValueOrDefault(t
	// .getiBondedArea()));
	// }
	//
	// StringBuilder pname = new StringBuilder();
	// StringBuilder pp = new StringBuilder();
	// StringBuilder pnum = new StringBuilder();
	// StringBuilder ci = new StringBuilder();
	//
	// boolean delimeter = false;
	// for (Ymt_OrderInfo orderInfo : orderInfos) {
	// GetProductNames(pname, orderInfo.getsTitle());
	// GetProductPrice(pp, orderInfo.getfOriginalPrice().toString());
	// GetProductNum(pnum,
	// DataUtil.Integerr.GetValueOrDefault(orderInfo.getiAmount())
	// .toString());
	// GetProductNos(orderInfo.getsSKU());
	//
	// if (delimeter) {
	// ci.append(';');
	// } else {
	// delimeter = true;
	// }
	// GetCatalogInfos(orderInfo.getsPropertyInfo());
	// }
	// }

	// private static void GetProductNames(StringBuilder pname, String s) {
	// if (pname.length() > 0) {
	// pname.append(';');
	// }
	// pname.append(DataUtil.Stringg.isNullOrEmpty(s) ? DataUtil.Stringg.Empty
	// : s.replace("？", ""));
	// }
	//
	// private static void GetProductPrice(StringBuilder pp, String s) {
	// if (pp.length() > 0) {
	// pp.append(';');
	// }
	// pp.append(DataUtil.Stringg.Empty);
	// }
	//
	// private static void GetProductNum(StringBuilder pno, String s) {
	// if (pno.length() > 0) {
	// pno.append(';');
	// }
	// pno.append(DataUtil.Stringg.Empty);
	// }

	private static String GetProductNos(String sku) {
		if (DataUtil.Stringg.isNullOrEmpty(sku))
			return DataUtil.Stringg.Empty;
		StringBuilder sb = new StringBuilder();
		for (String z : sku.split(";")) {
			if (DataUtil.Stringg.isNullOrEmpty(z))
				continue;
			if (sb.length() > 0) {
				sb.append(';');
			}
			sb.append(z.contains(":") ? z.substring(0, z.indexOf(":")) : z);
		}
		return sb.toString();
	}

	private static String GetCatalogInfos(String propertyInfo) {
		if (DataUtil.Stringg.isNullOrEmpty(propertyInfo)) {
			return DataUtil.Stringg.Empty;
		}
		StringBuilder sb = new StringBuilder();
		int n;
		for (String z : propertyInfo.split(",")) {
			if (DataUtil.Stringg.isNullOrEmpty(z))
				continue;

			n = z.lastIndexOf(':');
			if (n + 1 == z.length())
				continue;

			if (sb.length() > 0) {
				sb.append(':');
			}

			String strSecondProperty = z.substring(n + 1);
			n = strSecondProperty.lastIndexOf('#');

			if (n >= 0) {
				if (n + 1 == strSecondProperty.length()) {
					sb.append(DataUtil.Stringg.Empty);
				} else {
					sb.append(strSecondProperty.substring(n + 1));
				}
			} else {
				sb.append(strSecondProperty);
			}
		}
		return sb.toString();
	}
}
